package com.uroad.jilinpda.util;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by 王喜航 on 2018/1/28.
 */

public class UIUtils {

    /**
     * 处理","分割的图片信息
     *
     * @param reportFiles
     * @return
     */
    public static List<String> getStringList(String reportFiles) {
        List<String> imageList = new ArrayList<String>();
        if (!TextUtils.isEmpty(reportFiles)) {
            if (reportFiles.contains(",")) {
                String[] iconFiles = reportFiles.split(",");
                Collections.addAll(imageList, iconFiles);
            } else {
                imageList.add(reportFiles);
            }
        }
        return imageList;
    }

    /**
     * 动态设置view的长宽比 比如按长宽比显示图片
     * @param view 需要设置的view
     * @param width 宽度
     * @param height 高度
     */
    public static void setViewScale(final View view, final int width, final int height) {
        ViewTreeObserver observer = view.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                ViewGroup.LayoutParams bannerParams = view.getLayoutParams();
                int widget = view.getWidth();
                int bannerHeight = widget * height / width;
                bannerParams.width = widget;
                bannerParams.height = bannerHeight;
                view.setLayoutParams(bannerParams);
            }
        });
    }

}
